package LeetCode.linkednode;

public class LC_160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode longList = headA;  // 假设headA长
        ListNode shortList = headB; // headB短
        int long_ = 0; // 记录长链表的长度
        int short_ = 0;// 记录短链表的长度
        int differ;    // 记录长度差
        while (longList!=null){  // 求headA长度
            longList = longList.next;
            long_++;
        }
        while (shortList!=null){ // 求headB长度
            shortList = shortList.next;
            short_++;
        }
        differ = long_ - short_;
        longList = headA;  // 分别让指针指回头结点
        shortList = headB;
        if(differ < 0){  // 判断headA 是否 比 headB长
            longList = headB;
            shortList = headA;
            differ = -differ;
        }
        while (differ != 0){ // 让长链表先走 长度差 步
            longList = longList.next;
            differ--;
        }
        while (longList!=shortList){ //同时移动
            longList = longList.next;
            shortList = shortList.next;
        }
        return longList;
    }
}
